//
//  ViewController.m
//  网络检测
//
//  Created by Mac on 16/3/1.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "ViewController.h"
#import "RealReachability.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [GLobalRealReachability startNotifier];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(networkChanged:)
                                                 name:kRealReachabilityChangedNotification
                                               object:nil];
    [self.view addSubview:self.button];
    
}
- (void)networkChanged:(NSNotification *)notification
{
    RealReachability *reachability = (RealReachability *)notification.object;
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    NSLog(@"currentStatus:%@",@(status));
    
}
- (void)buttonClick {
    [GLobalRealReachability reachabilityWithBlock:^(ReachabilityStatus status) {
        switch (status)
        {
            case RealStatusNotReachable:
            {
                //  case NotReachable handler
                [_button setTitle:@"网络无法连接" forState:UIControlStateNormal];
                break;
            }
                
            case RealStatusViaWiFi:
            {
                //  case WiFi handler
                
                [_button setTitle:@"正在使用WiFi" forState:UIControlStateNormal];
                break;
            }
                
            case RealStatusViaWWAN:
            {
                //  case WWAN handler
                
                [_button setTitle:@"正在使用数据流量" forState:UIControlStateNormal];
                break;
            }
                
            default:
                break;
        }
    }];

}
- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.backgroundColor = [UIColor colorWithRed:0 green:0.45 blue:1 alpha:1];
        [_button setTitle:@"网络检测" forState:UIControlStateNormal];
        _button.bounds = CGRectMake(0, 0, 220, 60);
        _button.center = self.view.center;
        _button.layer.cornerRadius = 5;
        [_button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

@end
